<template>
  <div class="login_container">
    <div class="login_box">
      <div class="avater_box">
        <img src="../../assets/logo.png" alt />
      </div>

      <!--表单-->
      <el-form ref="LoginFormRef" :rules="LoginFormRules" :model="loginForm" class="login_form">
        <el-form-item prop="username">
          <el-input prefix-icon="iconfont icon-user" v-model="loginForm.username"></el-input>
        </el-form-item>
        <el-form-item>
          <el-input
            prefix-icon="iconfont icon-password"
            v-model="loginForm.password"
            type="password"
          ></el-input>
        </el-form-item>
        <el-form-item class="btns">
          <el-button type="primary" @click="submitForm">立即创建</el-button>
          <el-button type="info" @click="formReset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { loginUser } from '../../network/login'

export default {
  name: 'login',
  methods: {
    formReset() {
      this.$refs.LoginFormRef.resetFields()
    },
    submitForm() {
      this.$refs.LoginFormRef.validate(async valid => {
        if (!valid) return false
        const { data: res } = await loginUser(this.loginForm)
        if (res.meta.status === 200) {
          this.$message({
            message: '登录成功',
            type: 'success'
          })
        }
        sessionStorage.setItem('token', res.data.token)
        this.$router.push('/home')
      })
    }
  },
  data() {
    return {
      loginForm: {
        username: 'admin',
        password: '123456'
      },
      LoginFormRules: {
        username: [
          { required: true, message: '请输入活动名称', trigger: 'blur' },
          { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ]
      }
    }
  }
}
</script>
<style lang='less' scoped>
.login_container {
  background: #2b4b6b;
  height: 100%;
}

.login_box {
  width: 450px;
  height: 300px;
  background: #ffffff;
  border-radius: 5px;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);

  .avater_box {
    height: 130px;
    width: 130px;
    border: 1px solid #eeeeee;
    background: #ffffff;
    border-radius: 50%;
    padding: 10px;
    box-shadow: 0 0 10 #dddddd;
    position: absolute;
    left: 50%;
    transform: (translate(-50%, -50%));

    img {
      height: 100%;
      width: 100%;
      border-radius: 50%;
      background: #eeeeee;
    }
  }
}

.btns {
  display: flex;
  justify-content: flex-end;
}

.login_form {
  position: absolute;
  width: 100%;
  padding: 20px;
  box-sizing: border-box;
  bottom: 0;
}
</style>
